package com.rbtnet.blog.web.admin;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.rbtnet.blog.core.model.User;
import com.rbtnet.blog.core.service.UserService;

/**
 * 
 * @author 1136009436@qq.com
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private UserService userService;
	
	/**
	 * 用户登录
	 * @param user
	 * @param view
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public ModelAndView login(User user,ModelAndView view,HttpServletRequest request){
		User resultUser=userService.login(user);
		if(resultUser==null){
			view.addObject("user", user);
			view.addObject("errorMsg", "用户名或密码错误！");
			view.setViewName("login");
		}else{
			HttpSession session=request.getSession();
			session.setAttribute("currentUser", resultUser);
			view.addObject("currentUser", resultUser);
			view.setViewName("success");
		}
		logger.error("Hello Error");
		logger.info("Hello Info");
		return view;
	}
	
	@RequestMapping("/all/{pageNum}/{pageSize}")
    public Map<String,Object> findAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){
        return userService.findAllUser(pageNum,pageSize);
    }
}
